#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include"message/save.h"
#include"VideoVoiceCall/videocall.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
#include<QListWidget>
#include<QKeyEvent>
#include<QNetworkAccessManager>
#include<QNetworkReply>
#include"global.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(const QString &myname, QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void keyPressEvent(QKeyEvent *event) override {
        if (event->modifiers() & Qt::AltModifier && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)){
            on_pushButton_clicked();
        }
    }
    void closeEvent(QCloseEvent *event) override{
        logOut();
        QWidget::closeEvent(event);
        deleteLater();
    }
    void resizeEvent(QResizeEvent *) override;

private slots:
    void on_pushButton_clicked();
    void on_pushButton_4_clicked() ;
    void send(const QString &data, const NewsType &n = NewsType::text, const QString &fileName = "");
    void on_pushButton_2_clicked() ;
    void on_lineEdit_returnPressed();
    void on_pushButton_3_clicked();
    void on_pushButton_5_clicked();
    void on_pushButton_7_clicked();

private:
    void connectTCP();//连接tcp
    void logOut();

    void xunfeixinghuo(const QString &s);//讯飞星火发送
    void xunfeiDispose(QNetworkReply* reply);//讯飞星火接收
    void onDataReceived();//接收别人发来的消息
    void establish(const QString &name);//创建新的联系人窗口与按钮
    void addChatFrame(const QString &linkman, const QString&, const UserType &, const NewsType &nt = NewsType::text, bool b = false);//添加一条消息
    void fileSaving(const QByteArray ba, const QString &name);
    void Find_user(QByteArray, ReqID, ErrorCodes);//查找用户并返回头像
    void toLoad();

private:
    Ui::MainWindow *ui;

    QTcpSocket *socket;
    // 缓存用于存储接收到的数据
    QByteArray cache;
    // QNetworkAccessManager *mm;
    QNetworkAccessManager *xunfei;
    QNetworkRequest xunfeinet;
    QJsonObject userObject;

    Message message;
    QHash<QString, QPushButton *> address_book;
    int row;

    VideoCall *vc = nullptr;
};
#endif // MAINWINDOW_H
